package com.cgs.query.service.impl

import com.cgs.query.config.QueryConfigurationAware
import com.cgs.query.configuration.QueryConfiguration
import com.cgs.query.domain.Project2Define
import com.cgs.query.handler.*
import com.cgs.query.service.AliasDefineNameVersionVO
import com.cgs.query.service.IProject2DefineService
import com.cgs.query.service.jpa.Project2DefineRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
open class Project2DefineServiceImpl : IProject2DefineService, UserInfoHolderAware, QueryConfigurationAware, SecurityAdapter {

    @Autowired
    private lateinit var project2DefineRepository: Project2DefineRepository
    private lateinit var userInfoHolder: UserInfoHolder
    private lateinit var queryConfiguration: QueryConfiguration

    override fun save(project2Define: Project2Define): Project2Define {
        return project2DefineRepository.save(project2Define)
    }

    override fun save(project2Defines: Iterable<Project2Define>?) {
        project2DefineRepository.save(project2Defines)
    }

    override fun existAlias(projectName: String, alias: String): Boolean {
        return project2DefineRepository.countByProjectNameAndAlias(projectName, alias) > 0L
    }

    override fun existProjectName(projectName: String): Boolean {
        return project2DefineRepository.countByProjectName(projectName) > 0L
    }

    override fun findById(id: Int): Project2Define? {
        return project2DefineRepository.findOne(id)
    }

    override fun findAliasDefineNameVersionVOByProjectName(projectName: String): List<AliasDefineNameVersionVO> {
        return project2DefineRepository.findAliasDefineNameVersionVOByProjectName(projectName)
    }

    override fun findByDefineIdAndProjectName(defineId: String, projectName: String): List<Project2Define>? {
        return project2DefineRepository.findByDefineIdAndProjectName(defineId, projectName)
    }

    override fun findByProjectName(projectName: String): List<Project2Define>? {
        return project2DefineRepository.findByProjectName(projectName)
    }

    @Transactional
    override fun deleteByProjectName(projectName: String) {
        project2DefineRepository.deleteByProjectName(projectName)
    }

    @Transactional
    override fun deleteByDefineId(defineId: String) {
        project2DefineRepository.deleteByDefineId(defineId)
    }

    @Permission
    override fun delete(id: Int) {
        project2DefineRepository.delete(id)
    }

    override fun setUserInfoHolder(userInfoHolder: UserInfoHolder) {
        this.userInfoHolder = userInfoHolder
    }

    override fun setQueryConfiguration(queryConfiguration: QueryConfiguration) {
        this.queryConfiguration = queryConfiguration
    }

    override fun adapter(methodName: String, args: Array<Any?>): PermissionObject? {
        return when (methodName) {
            "delete" -> {
                val project2Define = findById(args[0] as Int)
                if (project2Define != null) {
                    PermissionObject(project2Define.projectName).apply {
                        this.userId = project2Define.userId
                        this.obj = project2Define
                        this.type = "project2Define"
                    }
                } else {
                    null
                }
            }
            else -> null
        }
    }

}